﻿/*
   Advanced Flash Engine in ActionScript 3.
   Copyright (C) 2009  Dean Gardiner

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.winmastergames.gameassets {
	import flash.display.*;
	import flash.events.*;
	import flash.display.Stage;
	import com.winmastergames.lolgame.mapDefault;
	import com.winmastergames.lolgame.enemys.*;
	import com.winmastergames.lolgame.gameUtils;
	import com.winmastergames.lolgame.engine;
	import flash.utils.Timer;

	public class enemySpawner extends MovieClip {
		//Set's the stage to mapDefault
		var myStage:mapDefault;
		//?
		var myMC:MovieClip;
		//var ammoRemaining:Number = 20;
		//Set's ref to mapDefault, writeX and writeY are the spawn x,y
		var spawnTimer:Timer;
		var spawnHealth:Number;

		public function enemySpawner(writeStage:mapDefault, writeX:Number, writeY:Number, health:Number, time:Number){
			spawnHealth = health;
			spawnTimer = new Timer(time * 1000);
			//Add's it to the objects movieclip in the stage
			writeStage.spawner.addChild(this);
			//Set's mystage to writestage dunno why.
			myStage = writeStage;
			//Set's it's x to the writePosition set in the function
			x = writeX;
			//Set's it's y to the writePosition set in the function
			y = writeY;
			//?
			myMC = MovieClip(this.parent.getChildByName(this.name));
			spawnTimer.addEventListener(TimerEvent.TIMER, spawnFunction);
			//spawnTimer.start();
			addEventListener(Event.ENTER_FRAME, loop);
		}

		public function spawnEnemy(levelRef:mapDefault, healthBarArrayRef:Array, enemyArrayRef:Array, xLoc:Number, yLoc:Number, hHealth:Number) {
			if (engine.st_enemyArray.length < engine.maxAi)
			{
				gameUtils.SpawnEnemy(levelRef,healthBarArrayRef,enemyArrayRef,xLoc,yLoc,hHealth)
				//trace("Enemy Spawned");
			}
				
			//trace("Total Enemies: " + engine.st_enemyArray.length);
		}

		function loop(e:Event){
			/*if (gameUtils.getState() == "pausemenu" || gameUtils.getState() == "invalidresolution" || gameUtils.getState() == "inmenu" || gameUtils.getState() == "console" || gameUtils.getState() == "inoptions"){
				spawnTimer.stop();
			} else {
				spawnTimer.start();
			}*/
		}

		function spawnFunction(e:Event){
			//spawnEnemy();
			spawnTimer.stop();
			spawnTimer.reset();
			spawnTimer.start();
		}
	}
}